package com.one.efaimaly.order.presenter;

import android.content.Context;

import com.one.common.model.extra.BaseExtra;
import com.one.common.model.http.callback.ObserverOnResultListener;
import com.one.common.presenter.BaseApiPresenter;
import com.one.common.utils.ImageFactory;
import com.one.common.utils.Logger;
import com.one.common.utils.MyToast;
import com.one.common.utils.StringUtils;
import com.one.common.utils.Toaster;
import com.one.common.utils.list.ListUtils;
import com.one.common.view.base.BaseActivity;
import com.one.efaimaly.order.model.OrderModel;
import com.one.efaimaly.order.model.extra.UploadImgExtra;
import com.one.efaimaly.order.view.UploadImgView;
import com.one.common.common.user.modle.UserInfoModel;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;

import static com.one.common.model.http.NetConstant.IMG_HOST;

/**
 * author ：zhulunjun
 * email  ：zhulunjun@qq.com
 * date   ：2018/10/20 下午7:12
 * description ： 上传维修图片
 **/
public class UploadImgPresenter extends BaseApiPresenter<UploadImgView, OrderModel> {

    private UploadImgExtra mExtra;
    private int size1,size2;
    private int sum1 = 0,sum2 = 0;
    private int uploadType;
    private static final int UPLOAD_1 =1;
    private static final int UPLOAD_2 =2;
    private StringBuffer urls1 = new StringBuffer(),urls2 = new StringBuffer();
    private ArrayList<String> urlArray2;
    public UploadImgPresenter(UploadImgView mView, Context mContext) {
        super(mView, mContext, new OrderModel((BaseActivity) mContext));
    }

    @Override
    public void onCreate() {
        super.onCreate();
        mExtra = (UploadImgExtra) mActivity.getIntent().getSerializableExtra(BaseExtra.getExtraName());
        if (mExtra != null && (StringUtils.isNotBlank(mExtra.getOrder_maintail_imgs1()) ||
                StringUtils.isNotBlank(mExtra.getOrder_maintail_imgs2()))) {
            // 显示图片
            mView.showImg(mExtra.getList1(), mExtra.getList2());
        }
    }


    /**
     * 上传图片
     */
    public void uploadImg(ArrayList<String> url1, ArrayList<String> url2) {
        if(ListUtils.isNotEmpty(url1)){
            size1 = url1.size();
            uploadType = UPLOAD_1;
        } else {
            Toaster.showLongToast("请选择未维修图片", MyToast.FAIL);
            return;
        }
        if(ListUtils.isNotEmpty(url2)){
            urlArray2 = url2;
            size2 = url2.size();
        } else {
            Toaster.showLongToast("请选择已维修图片", MyToast.FAIL);
            return;
        }

        //1. 先上传未维修的
        getUploadUrl(url1);

    }

    private void getUploadUrl(ArrayList<String> imgs){
        File file;
        for (String img : imgs){
            file = new File(img);
            if(file.exists()){
                uploadImg(file);
            }
        }
    }


    /**
     * 上传图片
     */
    private void uploadImg(File file) {
        if (file.exists()) {
            new UserInfoModel(mActivity).uploadImg(getImageFile(file.getPath()), new ObserverOnResultListener<String>() {
                @Override
                public void onResult(String s) {
                    String url = IMG_HOST + s;
                    Logger.d("zlj  图片地址 " + url);
                    // 未维修图片上传
                    if(uploadType == UPLOAD_1 && sum1 < size1){
                        sum1++;
                        urls1.append(url);
                        if(sum1 != size1){
                            urls1.append(",");
                        } else {
                            // 未维修图片上传完毕
                            // 2. 再上传已维修的
                            if(ListUtils.isNotEmpty(urlArray2)) {
                                getUploadUrl(urlArray2);
                                uploadType = UPLOAD_2;
                            }
                        }
                    } else if(uploadType == UPLOAD_2 && sum2 < size2){
                        sum2++;
                        urls2.append(url);
                        if(sum2 != size2){
                            urls2.append(",");
                        } else {
                            // 上传完毕
                            submit();
                        }
                    }
                }
            });
        }

    }
    /**
     * 缩放图片
     */
    private File getImageFile(String path){
        String outFilePath = mContext.getExternalCacheDir().getPath() + System.currentTimeMillis() + ".jpg";
        ImageFactory imageFactory = new ImageFactory();
        try {
            imageFactory.ratio(imageFactory.getBitmap(path), 480, 480, outFilePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return new File(outFilePath);
    }


    /**
     * 提交
     */
    private void submit(){
        mModel.uploadImg(mExtra.getOrderId(), urls1.toString(), urls2.toString(),
                new ObserverOnResultListener<String>() {
            @Override
            public void onResult(String s) {
                if(mView!= null){
                    mView.finishPage();
                }
            }
        });
    }


}
